package org.ndx.jsg.output.freemarker;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class Stats {
	private static final NumberFormat format = new DecimalFormat("00");

	private Map<String, Map<String, Map<String, Collection<FileWritingInfos>>>> timeStats;

	public Stats(SortedSet<FileWritingInfos> infos,
			List<FileWritingInfos> timeline) {
		timeStats = createTemporalStats(timeline);
	}

	/**
	 * Build a map linking years to map of months to map of days to list of entries
	 * @param timeline
	 */
	private Map<String, Map<String, Map<String, Collection<FileWritingInfos>>>> createTemporalStats(List<FileWritingInfos> timeline) {
		Map<String, Map<String, Map<String, Collection<FileWritingInfos>>>> returned = new TreeMap<String, Map<String, Map<String, Collection<FileWritingInfos>>>>();
		for(FileWritingInfos i : timeline) {
			Date writing = i.getWritingDate();
			// years start in 1900
			String writingYear = Integer.toString(writing.getYear()+1900);
			String writingMonth = format.format(writing.getMonth()==0 ? 12 : writing.getMonth());
			String writingDay = Integer.toString(writing.getDay());
			if(!returned.containsKey(writingYear)) {
				returned.put(writingYear, new TreeMap());
			}
			Map<String, Map<String, Collection<FileWritingInfos>>> year = returned.get(writingYear);
			if(!year.containsKey(writingMonth)) {
				year.put(writingMonth, new TreeMap());
			}
			Map<String, Collection<FileWritingInfos>> month = year.get(writingMonth);
			if(!month.containsKey(writingDay)) {
				month.put(writingDay, new TreeSet<FileWritingInfos>());
			}
			month.get(writingDay).add(i);
		}
		return returned;
	}

	public Map<String, Map<String, Map<String, Collection<FileWritingInfos>>>> getTimeStats() {
		return timeStats;
	}

}
